/********************************************************************
* Thank friend GG_ruler for his help 
* Thank friend GG_ruler for his help 
* Thank friend GG_ruler for his help 
* Thank friend GG_ruler for his help 
* @brief		    
    This file is used to write menu related programs
* @author           Chuyan

********************************************************************/
#include "Menu.h"


int16 Menu_Location; 
int16 Menu_PaperFlag,  Menu_IndexFlag, Menu_AddFlag, Menu_Paper, Menu_ReduFlag; 
float Servo_PWM = SERVO_MEDIAN;
float target_direction, target_distance, target_next_dis, target_next_dir, target_last_dis;//target angle  target distance

//-------------------------------------------------------------------------------------------------------------------
// @brief           menu display
// @param		    
// @return		              
// @since           v1.0
// Sample usage:    DisplayMenu();
//-------------------------------------------------------------------------------------------------------------------
void DisplayMenu()
{
    Key_Judge();    //the key to judge
	show_str(120,(Menu_Location)*MENU_GRID,"*");   //indecates the cursor
    switch(Menu_Paper)
    {
    case 0:DisplayMenu_0();break;
    case 1:DisplayMenu_1();break;
    case 2:DisplayMenu_2();break;
    case 3:DisplayMenu_3();break;
    }
}

//-------------------------------------------------------------------------------------------------------------------
// @brief           menu page 1 
// @param		     
// @return		               
// @since           v1.0
// Sample usage:    DisplayMenu_0()
//-------------------------------------------------------------------------------------------------------------------
void DisplayMenu_0()
{
        show_str(0, 0*MENU_GRID,"V_duty");      show_int32(50,0*MENU_GRID,V_duty, 2);
        show_str(0, 1*MENU_GRID,"S_PWM");       show_float(50,1*MENU_GRID,Servo_PWM, 4, 1);
        
        show_str(0, 2*MENU_GRID,"S_g.P");       show_float(50,2*MENU_GRID,Servo_gyro_pid.P, 2, 3);
        show_str(0, 3*MENU_GRID,"S_g.D");       show_float(50,3*MENU_GRID,Servo_gyro_pid.D, 2, 3);
        
        show_str(0, 4*MENU_GRID,"auto");        show_int32(80,4*MENU_GRID,STATE_auto_get, 1);
        
        show_str(0, 5*MENU_GRID,"S_A.P");       show_float(50,5*MENU_GRID,Servo_Angle_pid.P, 2, 3);
        show_str(0, 6*MENU_GRID,"S_A.D");       show_float(50,6*MENU_GRID,Servo_Angle_pid.D, 2, 3);
        
        show_str(0, 7*MENU_GRID,"dis");         show_float(50,7*MENU_GRID,hall_dis, 3, 1);
        show_str(0,8*MENU_GRID,"sate");         show_int32(50,8*MENU_GRID,gps_tau1201.satellite_used, 2); 
        show_str(0, 9*MENU_GRID,"t_dis");       show_float(50,9*MENU_GRID,target_distance,3,2);      //distance from the target point
        
}

//-------------------------------------------------------------------------------------------------------------------
// @brief		    menu page 1
// @param		    
// @return		              
// @since		    v1.0
// Sample usage:	DisplayMenu_1();			
//-------------------------------------------------------------------------------------------------------------------
void DisplayMenu_1(void)
{
    show_str(0, 0*MENU_GRID,"GPS.i");        show_int32(50,0*MENU_GRID,GPS_Data.i, 3);                  //the point I
    show_str(0, 1*MENU_GRID,"lat");          show_float(25,1*MENU_GRID,GPS_Data.lat[GPS_Data.i],3,6);   //current target latitude
    show_str(0, 2*MENU_GRID,"lon");          show_float(25,2*MENU_GRID,GPS_Data.lon[GPS_Data.i],3,6);   //current target longitude
    show_str(0, 3*MENU_GRID,"t_dis");        show_float(50,3*MENU_GRID,target_distance,3,2);      //distance from the target point
    
    show_str(0, 7*MENU_GRID,"Gyro");       show_float(30,7*MENU_GRID,WIT.WITangle_gyro_z,3,2);
    show_str(0,8*MENU_GRID,"sate");         show_int32(50,8*MENU_GRID,gps_tau1201.satellite_used, 2); 
    
} 

//-------------------------------------------------------------------------------------------------------------------
// @brief		    menu page 2
// @param		    
// @return		              
// @since		    v1.0
// Sample usage:	DisplayMenu_2();			
//-------------------------------------------------------------------------------------------------------------------
void DisplayMenu_2(void)
{
    if(gps_tau1201_flag)
    {  
        show_str(0,0*MENU_GRID,"state");    show_int32(50,0*MENU_GRID,gps_tau1201.state, 1);        //GPS receiving state 1: successful 0:failure
        show_str(0,1*MENU_GRID,"lat");      show_float(22,1*MENU_GRID,gps_tau1201.latitude,3, 6);   //GPS now latitude
        show_str(0,2*MENU_GRID,"lon");      show_float(22,2*MENU_GRID,gps_tau1201.longitude,3, 6);  //GPS now longitude
        
        show_str(0,3*MENU_GRID,"V");        show_float(50,3*MENU_GRID,gps_tau1201.speed,2,2); 
        show_str(0,4*MENU_GRID,"dir");      show_float(50,4*MENU_GRID,gps_tau1201.direction,3,2); 
        show_str(0,5*MENU_GRID,"sate");     show_int32(50,5*MENU_GRID,gps_tau1201.satellite_used, 2); 
        show_str(0,6*MENU_GRID,"hei");      show_float(50,6*MENU_GRID,gps_tau1201.height,3,2); 
    } 
    
}

//-------------------------------------------------------------------------------------------------------------------
// @brief		    menu page 3
// @param		    
// @return		              
// @since		    v1.0
// Sample usage:	DisplayMenu_3();			
//-------------------------------------------------------------------------------------------------------------------
void DisplayMenu_3(void)
{
//    show_str(0,0*MENU_GRID,"n_dir");    show_float(60,0*MENU_GRID,gps_tau1201.direction,3,2); 
//    show_str(0,1*MENU_GRID,"t_dir");    show_float(60,1*MENU_GRID,target_direction,3,2); 
//    show_str(0,2*MENU_GRID,"A_tar");    show_float(60,2*MENU_GRID,Aoa_tar,3,2); 
//    show_str(0,3*MENU_GRID,"i");        show_float(60,3*MENU_GRID,GPS_Data.i,3,2); 
//    show_str(0,4*MENU_GRID,"t_dis");    show_float(60,4*MENU_GRID,target_distance,3,2); 
//    show_str(0,5*MENU_GRID,"gyro");     show_float(60,5*MENU_GRID,WIT.WITangle_gyro_z,3,2);
//    show_str(0,6*MENU_GRID,"dis");      show_float(60,6*MENU_GRID,hall_dis,3,2);

        // debug interface
    {
        show_int32(0,0*MENU_GRID,SBUS.CH[0],4);show_int32(50,0*MENU_GRID,SBUS.CH[1],4);
        show_int32(0,1*MENU_GRID,SBUS.CH[2],4);show_int32(50,1*MENU_GRID,SBUS.CH[3],4);
        show_int32(0,2*MENU_GRID,SBUS.CH[4],4);show_int32(50,2*MENU_GRID,SBUS.CH[5],4);
        show_int32(0,3*MENU_GRID,SBUS.CH[6],4);show_int32(50,3*MENU_GRID,SBUS.CH[7],4);
        
        show_int32(50,5*MENU_GRID,time_k,4);
        show_str(0, 8*MENU_GRID,"RC.STATE"); show_int32(80,8*MENU_GRID,RC.STATE, 3);
    }
}
//-----------------------------------------------------------------------------------------------------------------------
// @brief		    
// @param		    
// @return		    
// @since		    v1.0
// Sample usage:	Key_Judge();			
//-------------------------------------------------------------------------------------------------------------------
void Key_Judge()
{
	if(gpio_get((KEY0)) == 1||gpio_get(KEY1) == 1||gpio_get(KEY2) == 1)
    {
        system_delay_ms(10);
        if(gpio_get((KEY0)) == 1||gpio_get(KEY1) == 1||gpio_get(KEY2) == 1) 
        {
            //////////////////////// ////////////////////////
            if(gpio_get((KEY0)) == 1) Menu_PaperFlag = 1;
            if((gpio_get((KEY0)) == 0) && Menu_PaperFlag) 
            {
                Menu_PaperFlag = 0;
				menu_clear();  
                Menu_Paper++;        
                if(Menu_Paper > 3) Menu_Paper = 0;
            }
            //////////////////////// ////////////////////////
            if(gpio_get(KEY1) == 1) Menu_IndexFlag = 1;
            if((gpio_get(KEY1) == 0) && Menu_IndexFlag)
            {
                Menu_IndexFlag = 0;
				menu_clear(); 
                Menu_Location++;  
                if(Menu_Location > 6) Menu_Location = 0;
            }
            ////////////////////////increase ////////////////////////
            if(gpio_get(KEY2) == 1) Menu_AddFlag = 1;
            if((gpio_get(KEY2) == 0) && Menu_AddFlag)
            {
                Menu_AddFlag = 0;
                switch(Menu_Paper)
                {
                case 0: // Zeroth page
                    switch (Menu_Location)
                    {
                    case 0: V_duty += V_GRID; if(V_duty > MAX_DUTY) V_duty = MAX_DUTY;break;
                    case 1: Servo_PWM+= SERVO_GRID; if(Servo_PWM > SERVO_UPPER) Servo_PWM = SERVO_UPPER;	break;
                    case 2: Servo_gyro_pid.P += PID_P_GRID; if(Servo_gyro_pid.P > 100) Servo_gyro_pid.P = 100;break;
                    case 3: Servo_gyro_pid.D += PID_P_GRID; if(Servo_gyro_pid.D > 100) Servo_gyro_pid.D = 100;break;
                    case 4:         
                        STATE_auto_get++;
                        if(STATE_auto_get > 1) STATE_auto_get = 0;break;
                    case 5: Servo_Angle_pid.P += PID_P_GRID; if(Servo_Angle_pid.P > 100) Servo_Angle_pid.P = 100;break;
                    case 6: Servo_Angle_pid.D += PID_P_GRID; if(Servo_Angle_pid.D > 100) Servo_Angle_pid.D = 100;break;
                    }break;
                case 1: // The first page
                    switch (Menu_Location)
                    {
                    case 0: GPS_Data.i++; if(GPS_Data.i > 90) GPS_Data.i = 0;break;
                    case 1:
                        {
//                            if(target_distance < (Point_Spacing-5)) ring(50);// distance is too far or too close
//                            else 
                            {
                                GPS_Data.lat[GPS_Data.i] = gps_tau1201.latitude;
                                GPS_Data.lon[GPS_Data.i] = gps_tau1201.longitude;
                                GPS_Data.i++;
                            }
                        }break;// GPS_inf_reset                        
                    }break;
                    // case 2: // The second page
                    //     switch (Menu_Location)
                    //     {
                    //     case 0:
                    //         {   // Read GPS data from flash memory   
                    //             // Disable the function of pressing a button to read the GPS flash, because the GPS flash can be read again each time the device is powered on  
                    //             get_flash_gps(FLASH_SECTION_GPS, FLASH_PAGE_LAT, FLASH_PAGE_LON);
                    //             for(GPS_Data.i = 1; GPS_Data.i < 200 && GPS_Data.lat[GPS_Data.i] != 0; GPS_Data.i++ )  
                    //             {   // Recalculate GPS collection point information 
                    //                 GPS_Data.former_i_dis[GPS_Data.i] = get_two_points_distance(GPS_Data.lat[GPS_Data.i-1], GPS_Data.lon[GPS_Data.i-1], GPS_Data.lat[GPS_Data.i], GPS_Data.lon[GPS_Data.i]); 
                    //                 GPS_Data.former_i_dir[GPS_Data.i] = get_two_points_azimuth(GPS_Data.lat[GPS_Data.i-1], GPS_Data.lon[GPS_Data.i-1], GPS_Data.lat[GPS_Data.i], GPS_Data.lon[GPS_Data.i]); 
                    //             }
                    //         } break;
                    //     }break;
                }
            }
            ////////////////////////reduce ////////////////////////
			if(gpio_get(KEY3) == 1)  Menu_ReduFlag = 1;
            if((gpio_get(KEY3) == 0) && Menu_ReduFlag) 
            {
                Menu_ReduFlag = 0;
                switch(Menu_Paper)
                {
                case 0: //Zeroth page
                    switch (Menu_Location) 
                    {
                    case 0: V_duty-= V_GRID; if(V_duty < 0) V_duty = 0;  break;
                    case 1: Servo_PWM -= SERVO_GRID; if(Servo_PWM < SERVO_LOWER) Servo_PWM = SERVO_LOWER; break;
                    case 2: Servo_gyro_pid.P -= PID_P_GRID; if(Servo_gyro_pid.P < 0) Servo_gyro_pid.P = 0; break;
                    case 3: Servo_gyro_pid.D -= PID_D_GRID; if(Servo_gyro_pid.D < 0) Servo_gyro_pid.D = 0;break;
                    
                    case 5: Servo_Angle_pid.P -= PID_P_GRID; if(Servo_Angle_pid.P < 0) Servo_Angle_pid.P = 0;break;
                    case 6: Servo_Angle_pid.D -= PID_P_GRID; if(Servo_Angle_pid.D < 0) Servo_Angle_pid.D = 0;break;
                    }break;
                case 1: //The first page
                    switch (Menu_Location) 
                    {
                    case 0: if(GPS_Data.i > 0) GPS_Data.i--;    break;
                    case 1:
                        {   
                            // Clear the current point and add 1
                            GPS_Data.lat[GPS_Data.i] = 0; 
                            GPS_Data.lon[GPS_Data.i++] = 0;   
                        }break; 
                    case 2:
                        {   
                            // Clear the current point
                            GPS_Data.lat[GPS_Data.i] = 0; 
                            GPS_Data.lon[GPS_Data.i] = 0;     
                        }break;
                    case 3: GPS_inf_reset();   break;   //reset the gps collection information 
                    }break;
                case 2: // The second page
                    switch (Menu_Location)
                    {       // Store GPS information in flash memory
                    case 0: store_gps_flash(FLASH_SECTION_GPS, FLASH_PAGE_LAT, FLASH_PAGE_LON);break;
                    }break;
				}
			}
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		}
	}
}

//-------------------------------------------------------------------------------------------------------------------
// @brief           buzzer ring
// @param           ms
// @return		    
// @since           v1.0
// Sample usage:    ring(50);		
//-------------------------------------------------------------------------------------------------------------------
void ring(int ms)
{   
    gpio_set(BUZZER_PIN,1);
    gpio_set(LED1,1);
    system_delay_ms(ms);
    gpio_set(BUZZER_PIN,0);
    gpio_set(LED1,0);
}
